/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DBHandling;

import Entity.Bill;
import Entity.BillDetail;
import Entity.Item;
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

/**
 *
 * @author NguyenPhuThien
 */
public class BillDetailHandling {
    private DBUtil objDB;

    public BillDetailHandling() {
        this.objDB = new DBUtil();
    }
    
    
    public Vector<BillDetail> getBillDetails(String date)throws Exception{
        try {
        String sql = "select b.*, i.itemName, i.price,bd.quantity from Items i, Bills b, BillDetails bd where i.itemID = bd.itemID AND b.billID = bd.billID  AND b.date ='"+date+"'";
        ResultSet rs =  objDB.executeSQL(sql);
        Vector<BillDetail> details= new Vector<BillDetail>();
        
        while(rs.next()){
            BillDetail bd = new BillDetail();
            Bill b = new Bill(rs.getString("billID"), rs.getString("username"),"", rs.getString("customerName"), rs.getString("payment"));
            bd.setBill(b);
            bd.setPrice(rs.getDouble("price"));
            bd.setQuantity(rs.getInt("quantity"));
            bd.setItemName(rs.getString("itemName"));
            details.add(bd);
        }
        
        objDB.closeConnection();
        return details;
        } catch (Exception ex) {
            throw ex;
        }
    }
    
    
    public boolean insertBillDetail(String billID, String itemID, int quantity) throws Exception {
        try {
            String sql = "{call sp_addBillDetail" + "(?, ?, ?)}";
            Object[] objects = {billID, itemID, quantity};
            return objDB.executeProcedureUpdate(sql, objects);
        } catch (Exception ex) {
            throw ex;
        }
    }
    
}
